﻿using System;
//using System.Collections.Generic;
//using System.ComponentModel;
//using System.Data;
//using System.Drawing;
//using System.Linq;
//using System.Text;
using System.Windows.Forms;

using System.Threading;

namespace pnh
{
    public partial class main : Form
    {
        public static string UrlHiddenAchievement = "http://www.eternal-apocalypse.fr/img/secret.jpg";
        public main()
        {
            InitializeComponent();
        }

        #region Console
        /// <summary>
        /// Delegate to the consoleWriteLine function
        /// </summary>
        /// <param name="str"></param>
        private delegate void consoleWriteLineDelegate(string str);

        /// <summary>
        /// Write a line in the console
        /// </summary>
        /// <param name="str"></param>
        public void consoleWriteLine(string str)
        {
            if (console.InvokeRequired)
            {
                consoleWriteLineDelegate d = new consoleWriteLineDelegate(consoleWriteLine);
                console.Invoke(d, new object[] { str + "\n" });
            }
            else
            {
                console.AppendText(str + "\n");
            }
        }

        #endregion

        #region Update Achievements Pictures

        private void updateAchievementsPicturesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Thread t = new Thread(new ThreadStart(threadingUpdateAchievementsPictures));
            t.IsBackground = true;
            t.Start();
        }

        private void threadingUpdateAchievementsPictures()
        {
            //create the update object
            UpdateAchievementsPictures up = new UpdateAchievementsPictures(consoleWriteLine, browser);

            //input box for the starting game id
            InputBox ib = new InputBox();
            ib.Label = "The starting game id : ";
            ib.Text = "Game ID";   
            string res = ib.showDialog();

            //parse the game id
            if (res == "")
                return;
            int iStartID = 0;
            try
            {
                iStartID = int.Parse(res);
            }
            catch(Exception e)
            {
                MessageBox.Show("Erreur : " + e.Message);
            }
             
            //start the update
            up.apply(iStartID);
            MessageBox.Show("Over");
        }

        #endregion

        #region Scan All Players
        private void scanAllPlayersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Thread t = new Thread(new ThreadStart(threadingScanAllPlayers));
            t.IsBackground = true;
            t.Start();
        }

        private void threadingScanAllPlayers()
        {
            Scan newScan = new Scan(consoleWriteLine, browser);
            newScan.scanIncAllPlayer();
            MessageBox.Show("Over");
        }
        #endregion

        #region Add Game
        private void addGameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Thread newThread = new Thread(new ThreadStart(threadingAddGame));
            newThread.IsBackground = true;
            newThread.Start();
        }

        private void threadingAddGame()
        {
            InputBox input = new InputBox();
            input.Text = "Game MSID";
            input.Label = "Game MSID : ";

            string msid = input.showDialog();
            if (msid == "")
                return;

            input.Text = "Player Gamertag";
            input.Label = "Player Gamertag : ";
            string gamertag = input.showDialog();
            if (gamertag == "")
                return;

            AddGame addGame = new AddGame(consoleWriteLine, browser);
            addGame.apply(msid, gamertag);
            MessageBox.Show("Over");
        }
        #endregion

        #region No Inc Scan Player
        private void noIncScanPlayerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Thread newThread = new Thread(new ThreadStart(threadingNoIncScanPlayer));
            newThread.IsBackground = true;
            newThread.Start();
        }

        private void threadingNoIncScanPlayer()
        {
            InputBox input = new InputBox();
            input.Text = "Gamertag";
            input.Label = "Gamertag : ";
            string gamertag = input.showDialog();
            if (gamertag == "")
                return;

            Scan newScan = new Scan(consoleWriteLine, browser);
            newScan.scanPlayer(gamertag);
            MessageBox.Show("Over");
        }
        #endregion

        #region Update Game Achievemnts Pictures
        private void updateGameAchievementsPicturesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Thread newThread = new Thread(new ThreadStart(threadingUpdateGameAchievementsPictures));
            newThread.IsBackground = true;
            newThread.Start();
        }

        private void threadingUpdateGameAchievementsPictures()
        {
            InputBox input = new InputBox();
            input.Text = "Game MSID";
            input.Label = "Game MSID : ";

            string msid = input.showDialog();
            if (msid == "")
                return;

            input.Text = "Player Gamertag";
            input.Label = "Player Gamertag : ";
            string gamertag = input.showDialog();
            if (gamertag == "")
                return;

            UpdateAchievementsPictures update = new UpdateAchievementsPictures(consoleWriteLine, browser);
            update.apply(msid, gamertag);
            MessageBox.Show("Over");
        }

        #endregion

        #region Connect
        private void connectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Thread newThread = new Thread(new ThreadStart(threadingConnectIncScan));
            newThread.IsBackground = true;
            newThread.Start();
        }

        private void threadingConnectIncScan()
        {
            Scan newScan = new Scan(consoleWriteLine, browser);
            newScan.connect();
        }
        #endregion

        private void testToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            try
            {
                string imageUrl = "https://live.xbox.com/tiles/3a/KJ/0zc8P2NhbC9CDxsCGltTVmU2L2FjaC8wLzU2AAAAAVBQUPymosE=.jpg";
                string src = @"C:\Users\Vash\Desktop\img\test.jpg";
                string dest = Constant.FTP_ACHE_IMG + "/559/49.jpg";

                //bool res = DBInterface.dowload(imageUrl, src);
                //if (!res)
                //{
                //    MessageBox.Show("No download");
                //    return;
                //}

                bool res = DBInterface.ftpUpload(src, dest);
                if (!res)
                {
                    MessageBox.Show("No upload");
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            MessageBox.Show("Picture saved.");
        }
    }
}
